package com.upb.webserver.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Getter
@ToString
@AllArgsConstructor
@Slf4j
public enum ChainOrAppStatusEnum {
    APPLYING_BY_DEVELOPER(0, "申请中"),
    WAIT_FOR_MANAGER_HANDLE(1, "待管理员处理"),
    REJECTED_BY_MANAGER(2, "工单被管理员退回"),
    ALREADY_TO_INITIALIZED(3, "准备初始化"),
    REMOTE_INITIALIZED(4, "远程初始化中"),
    REMOTE_DEPLOYING(5, "远程部署中"),
    DEPLOY_FAIL(6, "部署失败"),
    RUNNING(7, "运行"),
    UN_NORMAL(8, "状态异常"),
    REMOTE_RESTARTING(9, "远程重启中"),
    REMOTE_UPGRADING(10, "远程升级中"),
    UPGRADING_FAILED(11, "升级失败");


    private byte id;
    private String description;


    ChainOrAppStatusEnum(int id, String description) {
        this.id = (byte) id;
        this.description = description;
    }

    /**
     * @param id
     * @return
     */
    public static ChainOrAppStatusEnum getById(byte id) {
        for (ChainOrAppStatusEnum value : ChainOrAppStatusEnum.values()) {
            if (value.id == id) {
                return value;
            }
        }
        return null;
    }


    /**
     * @return
     */
    public OrderStatusEnum convertToOrderStatusEnum() {
        switch (this) {
            case APPLYING_BY_DEVELOPER:
                return OrderStatusEnum.APPLYING_BY_DEVELOPER;
            case WAIT_FOR_MANAGER_HANDLE:
                return OrderStatusEnum.WAIT_FOR_MANAGER_HANDLE;
            case REJECTED_BY_MANAGER:
                return OrderStatusEnum.REJECTED_BY_MANAGER;
            case ALREADY_TO_INITIALIZED:
            case REMOTE_INITIALIZED:
            case REMOTE_DEPLOYING:
            case REMOTE_RESTARTING:
            case REMOTE_UPGRADING:
                return OrderStatusEnum.HANDLING_BY_MANAGER;
            case DEPLOY_FAIL:
            case UPGRADING_FAILED:
                return OrderStatusEnum.HANDLE_FAIL_BY_MANAGER;
            case RUNNING:
            case UN_NORMAL:
                return OrderStatusEnum.HANDLE_SUCCESS_BY_MANAGER;
        }
        log.error("fail exec method[convertToOrderStatusEnum]. not config status:{} desc:{}", this.getId(), this.getDescription());
        return null;
    }

}